import matplotlib.pyplot as plt
import math
# 第一步--画出田块边界
# 第二步--确定回转次数  田块宽度除以收割幅宽
# 第三步--画出一次偏置
# 第四步--确定整体回转路径图


class Roundroute():
    def __init__(self, length_field=int(), width_field=int(), harvest_width=3, turn_radius=6):
        self.length_field = length_field
        self.width_field = width_field
        self.harvest_width = harvest_width
        self.turn_radius = turn_radius
        self.data = [[(0, 0), (0, self.width_field), (self.length_field, self.width_field), (self.length_field, 0)]]

# 确定回转型路径的圈数
    def num_round(self):
        if self.length_field >= self.width_field:
            num_round = self.width_field % (2 * self.harvest_width)
            if num_round < self.harvest_width:
                fq = int((self.width_field / (2 * self.harvest_width)))
            else:
                fq = int((self.width_field / (2 * self.harvest_width)))
        else:
            num_round = self.length_field % (2 * self.harvest_width)
            if num_round < self.harvest_width:
                fq = int((self.length_field / (2 * self.harvest_width)))
            else:
                fq = int((self.length_field / (2 * self.harvest_width)))
        return fq

# boundary_point函数功能：返回二维坐标的x、y点的坐标集合
    def boundary_point(self, data=None):
        x_data = []
        for i in range(len(data)):  # 返回x列表中的元素
            x_data.append(data[i][0])
        y_data = []
        for i in range(len(data)):
            y_data.append(data[i][1])
        return x_data, y_data  # 输出参数 x，y组成的坐标集合  x = (0, 0),  y = (0, 1)

# 画出田块边界
    def boundary(self):
        plt.rcParams['font.sans-serif'] = ['SimHei']
        plt.rcParams['axes.unicode_minus'] = False
        global x_one
        global y_one
        x_one = []
        y_one = []
        for i in range(len(self.data)):
            x_one, y_one = self.boundary_point(data=self.data[i])
        x_one.append(x_one[0])  # 增加第一个坐标使得矩形封闭
        y_one.append(y_one[0])  # 五个坐标点使得图形封闭
        return x_one, y_one

# 得到每一次向内偏置后五个坐标点组成的列表
    def first_bias_list(self, num_point=None, num_i=None):
        for i in range(1, self.num_round()+1):
            x_bias_one = x_one[0] + i * self.harvest_width     # 每次偏置后组成的点列表
            x_bias_two = x_bias_one
            x_bias_three = x_one[2] - i * self.harvest_width
            x_bias_four = x_bias_three
            x_bias_five = x_bias_one
            y_bias_one = y_one[0] + i * self.harvest_width
            y_bias_two = y_one[1] - i * self.harvest_width
            y_bias_three = y_bias_two
            y_bias_four = y_bias_one
            y_bias_five = y_bias_one
            x_one.append(x_bias_one)
            x_one.append(x_bias_two)
            x_one.append(x_bias_three)
            x_one.append(x_bias_four)
            x_one.append(x_bias_five)
            y_one.append(y_bias_one)
            y_one.append(y_bias_two)
            y_one.append(y_bias_three)
            y_one.append(y_bias_four)
            y_one.append(y_bias_five)
        self.divide_axis(x_one_group=x_one, y_one_group=y_one)
        # print(self.divide_x_one)
        # print(self.divide_y_one)
        # return x_one, y_one

    def divide_axis(self, x_one_group=None, y_one_group=None):
        x_one_groups = zip(*(iter(x_one_group),) * 5)
        y_one_groups = zip(*(iter(y_one_group),) * 5)
        self.divide_x_one = [list(i) for i in x_one_groups]
        self.divide_y_one = [list(i) for i in y_one_groups]
        count = len(y_one_group) % 5
        self.divide_x_one.append(x_one_group[-count:]) if count != 0 else self.divide_x_one
        self.divide_y_one.append(y_one_group[-count:]) if count != 0 else self.divide_y_one
        # return divide_x_one, divide_y_one

# 画出偏置后的矩形框图
    def bias_route_visual(self):
        plt.plot(self.divide_x_one[0], self.divide_y_one[0], c='k')
        for i in range(1, len(self.divide_x_one)):
            plt.plot(self.divide_x_one[i], self.divide_y_one[i], c='r')
        plt.title('完成收获所需要的转弯次数为：' + str(self.num_round() * 4) + '次')
        plt.axis('off')
        plt.show()
        # print(self.length_radius(data=self.num_radius(data=self.divide_x_one), line_line_radius=1))

# 计算整田块回转路径的转弯次数
    def num_round_radius(self):
        num_radius = None
        if self.length_field >= self.width_field and self.width_field % (2 * self.harvest_width) < self.harvest_width:
            num_radius = 4 * self.num_round()
        elif self.length_field >= self.width_field and self.width_field % (2 * self.harvest_width) > self.harvest_width:
            num_radius = 4 * self.num_round() + 2
        elif self.length_field < self.width_field and self.length_field % (2 * self.harvest_width) < self.harvest_width:
            num_radius = 4 * self.num_round()
        elif self.length_field < self.width_field and self.length_field % (2 * self.harvest_width) > self.harvest_width:
            num_radius = 4 * self.num_round() + 2
        return num_radius

# 计算田块回转式转弯路径总长度
    def length_radius(self):
        # length_radius = data * (6 + 3 + 0.5 * math.pi * 6) + line_line_radius * ((2 * self.turn_radius) * (math.pi - 2 * (math.atan(self.harvest_width / ((4 * self.turn_radius ** 2 - self.harvest_width ** 2) ** 0.5)))))
        length_radius = self.num_round_radius() * ((math.pi * self.turn_radius) / 2)
        return length_radius

# 计算整个农田收获所需要的全路径长度
    def entire_length(self):
        a1 = 2 * (self.length_field + self.width_field)
        tolerance = (0 - (2 * self.harvest_width) * 4)
        straight_length = None
        if self.length_field >= self.width_field and self.width_field % (2 * self.harvest_width) <= self.harvest_width:
            straight_length = self.num_round() * a1 + ((self.num_round() * (self.num_round() - 1))/2) * tolerance + (((a1 + self.num_round() * tolerance) - 2 * (self.width_field % (2 * self.harvest_width))) / 2)
        elif self.length_field >= self.width_field and self.width_field % (2 * self.harvest_width) > self.harvest_width:
            straight_length = (self.num_round() + 1) * a1 + (((self.num_round() + 1) * self.num_round())/2) * tolerance
        elif self.length_field < self.width_field and self.length_field % (2 * self.harvest_width) <= self.harvest_width:
            straight_length = self.num_round() * a1 + ((self.num_round() * (self.num_round() - 1)) / 2) * tolerance + (
                        ((a1 + self.num_round() * tolerance) - 2 * (self.length_field % (2 * self.harvest_width))) / 2)
        elif self.length_field < self.width_field and self.length_field % (2 * self.harvest_width) > self.harvest_width:
            straight_length = (self.num_round() + 1) * a1 + (
                        ((self.num_round() + 1) * self.num_round()) / 2) * tolerance
        entire_length = self.length_radius() + straight_length
        return entire_length


roundroute = Roundroute(83, 46, 3, 6)
roundroute.boundary()
roundroute.first_bias_list()
roundroute.bias_route_visual()
print('回转式路径的总长度为：' + str(roundroute.entire_length()))
